package com.smbms.controller;

import com.alibaba.fastjson.JSON;
import com.smbms.pojo.Bill;
import com.smbms.pojo.Provider;
import com.smbms.pojo.User;
import com.smbms.servlet.BillService;
import com.smbms.servlet.ProviderService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bill")
public class BillController {

    @Resource
    private BillService billService;
    @Resource
    private ProviderService providerService;

    @RequestMapping("/updateBill")
    public String updateBill(@ModelAttribute Bill bill ,HttpSession session){
        User loginUser=(User)session.getAttribute("loginUser");
        bill.setModifyBy(loginUser.getId());
        bill.setModifyDate(new Date());
        boolean resout = billService.updateBill(bill);
        if(resout){
            return "redirect:/bill/billList";
        }else {
            return "billmodify";
        }
    }

    /*删除订单信息*/
    @RequestMapping("/delete/{billid}")
    @ResponseBody/*ajax注解*/
    public String deleteById(@PathVariable Integer billid){
        Map<String,Object> map = new HashMap<String,Object>();
        int row = billService.deleteVyId(billid);
        if(row > 0){
            map.put("delResult","true");
        }else if(row == 0){
            map.put("delResult","notexist");
        }else {
            map.put("delResult","false");
        }
        return JSON.toJSONString(map);
    }

    /*修改订单信息*/
    /*/bill/modifBillShow/"+ obj.attr("billid")*/
    @RequestMapping("/modifBillShow/{billid}")
    public String modifBillShow(@PathVariable Integer billid,HttpServletRequest request){
        /*先查询供应商表信息*/
        List<Provider> providerList = providerService.getProvider();
        /*根据id查询订单信息*/
        Bill bill = billService.getBillById(billid);
        if(bill != null){
            request.setAttribute("providerList",providerList);
            request.setAttribute("bill",bill);
            return "billmodify";
        }
        return "redirect:bill/billList";

    }

    /*查看订单信息*/
    @RequestMapping("/billView/{billid}")
    public String billView(HttpServletRequest request, @PathVariable Integer billid){
        Bill bill = billService.getBillById(billid);
        if(bill != null){
            request.setAttribute("bill",bill);
            return "billview";
        }
        return "redirect:/bill/billList";
    }

    @RequestMapping("/billAdd")
    public String billAdd(@ModelAttribute Bill bill, HttpSession session){
       User loginUser = (User)session.getAttribute("loginUser");
       bill.setCreatedBy(loginUser.getId());
       bill.setCreationDate(new Date());
       boolean resout = billService.addBill(bill);
       if(resout){
           return "redirect:/bill/billList";
       }
        return "redirect:/bill/billAddShow";
    }

    @RequestMapping("/billAddShow")
    public String billAddShow(HttpServletRequest request){
        /*先吧所有供应商信息查询出来*/
       List<Provider> providerList = providerService.getProvider();
       request.setAttribute("providerList",providerList);
       return "billadd";
    }

    @RequestMapping("/billList")
    public String billList(HttpServletRequest request,String queryProductName,Integer queryProviderId,Integer queryIsPayment){
        /*同样参数太多了，放在map集合中*/
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("queryProductName",queryProductName);
        map.put("queryProviderId",queryProviderId);
        map.put("queryIsPayment",queryIsPayment);
        System.out.println(queryProductName);
        System.out.println(queryProviderId);
        System.out.println(queryIsPayment);
        /*先吧所有供应商信息查询出来*/
        List<Provider> providerList = providerService.getProvider();
        request.setAttribute("providerList",providerList);
        List<Bill> bill = billService.getBillList(map);
        if(bill != null){
            request.setAttribute("billList",bill);
            request.setAttribute("billList",bill);
            request.setAttribute("queryProductName",queryProductName);
            request.setAttribute("queryProviderId",queryProviderId);
            request.setAttribute("queryIsPayment",queryIsPayment);
            return "billlist";
        }
        return "frame";
    }
}
